package com.lyxs916.testlibrary.uploding;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.lyxs916.appbaselibrary.core.activity.BaseActivity;
import com.lyxs916.appbaselibrary.core.http.BaseHttpCallback;
import com.lyxs916.appbaselibrary.core.utils.dialog.DialogProgress;
import com.lyxs916.appbaselibrary.core.utils.gilde.GlideUtils;
import com.lyxs916.common.HeadType;
import com.lyxs916.common.HostType;
import com.lyxs916.network.BaseHttpRequest;
import com.lyxs916.network.HttpCallBack;
import com.lyxs916.progresscallback.ProgressCallBack;
import com.lyxs916.requestbody.HttpRequestParam;
import com.lyxs916.testlibrary.R;
import com.lyxs916.utilslibrary.view.SupperToolBar;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import okhttp3.ResponseBody;

/**
 * 说明:上传文件的测试
 * 作者： 杨阳; 创建于：  2017-06-15  10:50
 */
public class UplodingActivity extends BaseActivity {
    Button selectPicture;
    ImageView picture;
    SupperToolBar toolbar;
    ArrayList<String> picturePath = new ArrayList<>();

    public static void startUi(Context context) {
        Intent intent = new Intent(context, UplodingActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(intent);
    }

    @Override
    public int setContentView() {
        return R.layout.test_activity_uploading;
    }
    @Override
    public void parseIntent(Intent intent) {
    }

    @Override
    public void initView() {
        toolbar = (SupperToolBar) findViewById(R.id.toolbar);
        selectPicture = (Button) findViewById(R.id.uploading);
        picture = (ImageView) findViewById(R.id.picture);
        toolbar.setTitle("上传文件");
        toolbar.setBack(this);
        //选择图片
        selectPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MultiImageSelector.create()
                        .showCamera(true)
                        .single()
                        .origin(picturePath)
                        .start(mActivity, REQUEST_CODE);
            }
        });
        //上传图片
        findViewById(R.id.uploading2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getHttpData(true);
            }
        });
    }


    DialogProgress dialogProgress;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //过程图片路径
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            picturePath.clear();
            picturePath.addAll(data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT));
            GlideUtils.show(findViewById(R.id.picture), picturePath.get(0));
        }
    }


    public void getHttpData(final boolean isFirst) {


        HttpCallBack.Builder buider = new BaseHttpCallback.BaseBuilder()
                .setShowLoading(this, isFirst)
                .setCancelable(true)
                .setHint("正在加载...")
                .setHttpHead(HeadType.LOGIN_HEAD)
                .setShowDefeatView(isFirst, null)
                .setHostType(HostType.TYPE_TWO);

        final BaseHttpCallback httpCallBack = new BaseHttpCallback<ResponseBody>(buider) {
            @Override
            public void onSuccess(final ResponseBody result) {
                super.onSuccess(result);
                if (result != null) {
                    try {
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };


//        Map<String, String> map = new HashMap<>();
//        map.put("state", 4 + "");
//        BaseHttpRequest.executePost(httpCallBack, "/nurse/orders.action", map);
/**
 * action	UserUpdateHead
 Avatar	lubanCache1497517457115.jpg
 UserParam	DDTFkpOjrWCNn93u8nm3AwaPwfJKZVMyoOOLeW9oVOGjgIG6h65jorKVkaiSnqlCreL9OjRbQrwR
 LZSU5CO46mm9G6JmazmHhXQQh3p/pIs9LCG3u3biZD+3c9IXuKNBNvqwHZDvJNg=
 */
///storage/emulated/0/image/Android_avatar_20170518102013Z9hEA.png
        HttpRequestParam param = new HttpRequestParam();
        File file = new File(picturePath.get(0));
        File fil2 = new File("storage/emulated/0/image/Android_avatar_20170518102013Z9hEA.png");
        param.addParam("action", "UserUpdateHead");
        param.addParam("UserParam", "DDTFkpOjrWCNn93u8nm3AwaPwfJKZVMyoOOLeW9oVOGjgIG6h65jorKVkaiSnqlCreL9OjRbQrwRLZSU5CO46mm9G6JmazmHhXQQh3p/pIs9LCG3u3biZD+3c9IXuKNBNvqwHZDvJNg=");
        param.addParam("Avatar", file);
        param.addParam("Avatar2", file);
        param.addParam("Avatar2", fil2);
//        BaseHttpRequest.uploadFiles(httpCallBack, "tools/app_api.ashx", param);

        //上传并实现回调接口
        BaseHttpRequest.executeCallBackLongTimeOutPost(httpCallBack, "tools/app_api.ashx", param, new ProgressCallBack() {
            @Override
            public void onLoading(long progress, long total, boolean done, int type) {
                Log.e("AAAAAAAAAAAAAAAAA",""+(progress + "----" + total + "-----" + (Double.parseDouble(progress+"")/ total)));

                int aa= (int) ((Double.parseDouble(progress+"")/ total)*100);

                if (dialogProgress == null) {
                    dialogProgress = new DialogProgress(mActivity);
                }
                dialogProgress.setTitleText( "上传中");
                dialogProgress.setProgress(aa);
                dialogProgress.show();
                if (done) {
                    dialogProgress.dismiss();
                }
            }

            @Override
            public long getRate() {
                return 10;
            }

        });


        Map<String, String> map = new HashMap<>();
        map.put("action", "UserUpdateHead");
        map.put("UserParam", "DDTFkpOjrWCNn93u8nm3AwaPwfJKZVMyoOOLeW9oVOGjgIG6h65jorKVkaiSnqlCreL9OjRbQrwR\n" +
                "LZSU5CO46mm9G6JmazmHhXQQh3p/pIs9LCG3u3biZD+3c9IXuKNBNvqwHZDvJNg=");

        Map<String, File> fmap = new HashMap<>();
        fmap.put("Avatar", file);
//        BaseHttpRequest.uploadFiles(httpCallBack, "tools/app_api.ashx", map,fmap);

    }


}
